package com.bill99.codecheck.sonar.orm.manager.impl;

import java.io.UnsupportedEncodingException;

import com.bill99.codecheck.sonar.orm.dao.SvnauthDataDao;
import com.bill99.codecheck.sonar.orm.entity.SvnauthItem;
import com.bill99.codecheck.sonar.orm.manager.SvnauthMng;
import com.bill99.riaframework.common.helper.Bill99Logger;

public class SvnauthMngImpl implements SvnauthMng {
	
	private SvnauthDataDao svnauthDataDao;
	private Bill99Logger logger = Bill99Logger.getLogger(this.getClass());


	public void setSvnauthDataDao(SvnauthDataDao svnauthDataDao) {
		this.svnauthDataDao = svnauthDataDao;
	}


	@Override
	public SvnauthItem getSvnInfo(String username) {
		SvnauthItem svnauthItem = svnauthDataDao.getSvnInfo(username);
		String fullname = convertCharset(svnauthItem.getFullname());
		svnauthItem.setFullname(fullname);
		return svnauthItem;
	}
	
	private String convertCharset(String s) {
		if (s != null) {
			try {
				int length = s.length();
				byte[] buffer = new byte[length];
				//0x81 to Unicode 0x0081, 0x8d to 0x008d, 0x8f to 0x008f, 0x90 to 0x0090, and 0x9d to 0x009d.
				for (int i = 0; i < length; ++i) {
					char c = s.charAt(i);
					if (c == 0x0081) {
						buffer[i] = (byte) 0x81;
					} else if (c == 0x008d) {
						buffer[i] = (byte) 0x8d;
					} else if (c == 0x008f) {
						buffer[i] = (byte) 0x8f;
					} else if (c == 0x0090) {
						buffer[i] = (byte) 0x90;
					} else if (c == 0x009d) {
						buffer[i] = (byte) 0x9d;
					} else {
						buffer[i] = Character.toString(c).getBytes("cp1252")[0];
					}
				}
				String result = new String(buffer, "utf-8");
				return result;
			} catch (UnsupportedEncodingException e) {
				logger.error("charset convert error", e);
			}
		}
		return null;
	}


}
